#include "Common.fx"
#include "Sampling.fx"

//--------------------------------------
struct a2v {
	float4 pos		: POSITION;
};

struct v2f {
	float4 pos		: POSITION;
	float2 uv		: TEXCOORD0;
};

//--------------------------------------
sampler g_InputSamp : register(s0);

//--------------------------------------
v2f vsMain(a2v input) {
	
	v2f output;
	
	output.pos = input.pos;
	
	output.uv.x =  input.pos.x;
	output.uv.y = -input.pos.y;
	output.uv = output.uv * 0.5 + 0.5;
	
	return output;
}

//--------------------------------------
f2fb psMain(v2f input) {

	f2fb output;
	
	float2 baseTexcoord = input.uv + g_InvShadowMapSize.y;
	
	float4 samples[9];
	
	for(int i=0; i<9; ++i)
		samples[i] = tex2D(g_InputSamp, baseTexcoord + g_Uniform3x3[i] * g_InvShadowMapSize.x);
	
	output.c0 = samples[4];
	
	if(output.c0.y > 0.999)
	{
		output.c0.y = min(output.c0.y, samples[0].y);
		output.c0.y = min(output.c0.y, samples[1].y);
		output.c0.y = min(output.c0.y, samples[2].y);
		output.c0.y = min(output.c0.y, samples[3].y);
		output.c0.y = min(output.c0.y, samples[5].y);
		output.c0.y = min(output.c0.y, samples[6].y);
		output.c0.y = min(output.c0.y, samples[7].y);
		output.c0.y = min(output.c0.y, samples[8].y);
	}
		
	return output;
}

//--------------------------------------
technique DownSample {

	pass {
	
		//ColorWriteEnable	= GREEN;
		AlphaBlendEnable	= false;
		AlphaTestEnable		= false;
		ZEnable				= false;
		ZWriteEnable		= false;
		
		VertexShader		= compile vs_3_0 vsMain();
		PixelShader			= compile ps_3_0 psMain();
	}
}

//--------------------------------------